package action.FrontAction;

import com.opensymphony.xwork2.ActionSupport;
import entity.CroBills;
import entity.CroCustomer;
import entity.CroPurchase;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroBillsService;
import service.CroCustomerService;
import service.CroPurchaseService;


public class payAction extends ActionSupport {

    @Autowired
    private CroPurchaseService croPurchaseService;
    @Autowired
    private CroBillsService croBillsService;
    @Autowired
    private CroCustomerService croCustomerService;

    private String purchaseId;
    private String jsonResult;

    /**
     * 支付页面
     */
    public String paylist(){
        return SUCCESS;
    }

    public String payI(){
//        ServletContext context = ServletActionContext.getServletContext();
        CroPurchase purchase = croPurchaseService.findById(purchaseId);
        CroCustomer customer = purchase.getCroCustomer();
        if (purchase.getPaytype().equals("2")) {
            double balance = customer.getBalance();
            balance = balance - purchase.getTotalprice();
            if (balance < 0) {
                jsonResult = "{\"status\":0,\"msg\":\"余额不足, 支付失败!\"}";
                return SUCCESS;
            } else {
                customer.setBalance(balance);
                croCustomerService.updateCustomer(customer);
            }
        }
        CroBills bill = new CroBills();
        bill.setCroCustomer(customer);
        bill.setConsumption(purchase.getTotalprice());
        bill.setBilltype("1");
        croBillsService.addBills(bill);
        purchase.setOrderStatus("2");
        if (croPurchaseService.updatePurchase(purchase)){
            jsonResult = "{\"status\":1,\"msg\":\"支付成功!\"}";
        }else {
            jsonResult = "{\"status\":0,\"msg\":\"支付失败!\"}";
        }
        return SUCCESS;
    }

    public String getPurchaseId() {
        return purchaseId;
    }

    public void setPurchaseId(String purchaseId) {
        this.purchaseId = purchaseId;
    }

    public String getJsonResult() {
        return jsonResult;
    }

    public void setJsonResult(String jsonResult) {
        this.jsonResult = jsonResult;
    }
}
